function createRandomStr(length) {
  const str = 'abcdefghijklmnopqrstuvwxyz0123456789._';
  const arr = new Array(length);
  const len = str.length - 1;
  let res = '';

  for (let i of arr) {
    const index = (Math.random() * len).toFixed();
    res += str[index];
  }

  return res;
}

const pxToRpx = (px) => {
  const width = wx.getSystemInfoSync().windowWidth;
  const R_WIDTH = 750;

  return px * (R_WIDTH / width);
};

function isObject (data) {
  return typeof data === 'object' && data !== null;
}

// 给对象做监听
function observer (data, cb) {
  if (!isObject(data)) {
    return;
  }

  for (let key in data) {
    defineProperty(data, key, data[key], cb);
  }
}

function defineProperty (data, key, value, cb) {
  observer(value);
  Object.defineProperty(data, key, {
    set (newValue) {
      if (value === newValue) {
        return;
      }

      observer(newValue);
      value = newValue;
      cb && cb(newValue);
    },
    get () {
      return value;
    }
  });
}

/**
 * 将当前时间格式化成指定的字符串
 * 
 * @param {*} format YYYY-MM-DD HH:MM:SS
 * @returns 
 */
function formatTime (format = '') {
  const now = new Date();

  const year = now.getFullYear();
  const month = now.getMonth() + 1;
  const date = now.getDate();
  const hour = now.getHours();
  const minute = now.getMinutes();
  const second = now.getSeconds();

  return format
          .replace('YYYY', year)
          .replace('MM', month)
          .replace('DD', date)
          .replace('HH', hour)
          .replace('MM', minute)
          .replace('SS', second);
}

function createFakeTasks () {
  
}


module.exports = {
  createRandomStr,
  pxToRpx,
  observer,
  formatTime,
  isObject
}